<?php

namespace app\wechat\controller;

use app\wechat\validate\PriceValidate;
use think\Db;

class Express extends Base
{
    //
    public function offer()
    {
        $user       = session("user");
        $time_info  = time() - 14400;
        $order_Info = model('order')->with('Users')
            ->where('status', 1)
            ->where('create_time', '>', $time_info)
            ->select();
        /**
         * 过滤掉已经出价的订单信息
         */
//        var_dump($order_Info);
        foreach ($order_Info as $key => $value) {
            $result = Db::name('price')
                ->where('order_num', $value['order_number'])
                ->where('user_id', $user['id'])->find();
            if ($result) {
                unset($order_Info[$key]);
            }
        }

        $userInfo = model('users')->find($user['id']);
        $viewData = [
            'user'  => $userInfo,
            'order' => $order_Info
        ];
        $this->assign($viewData);
        return view();
    }

    public function offer_user()
    {
        return view();
    }

    public function send()
    {
        if (request()->isAjax()) {
            $orderInfo = model('order')->where('order_number',input('order_num'))->find();
            $user = session("user");
            $data = input('post.');
            /**
             * 校验数据是否填写正确
             */
            $ret = $this->validate($data, PriceValidate::class);
            if ($ret !== true) {
                $this->error($ret);
            }
            $data['user_id']     = $user['id'];
            $data['create_time'] = time();
            $result              = Db::name('price')->strict(false)->insertGetId($data);
            $wechat              = new Message();
            $apply_user          = Db::name('users')
                ->where('id', $orderInfo['user_id'])
                ->find();
            $wechat->PriceToApply($apply_user['openid'], $apply_user['real_name'], $orderInfo['goods'], $result, $data['company_name']);
            if ($result) {
                $this->success('报价成功!', 'wechat/index/index');
            } else {
                $this->error('报价失败!');
            }
        }
        $orderInfo = model('order')->find(input('id'));
        $viewData = [
            'order' => $orderInfo
        ];
        $this->assign($viewData);
        return view();
    }

    //已经报价的列表
    public function sendList()
    {
        $user       = session("user");
        $price_info = model('price')::with('Order')->where('user_id', $user['id'])->select();
        $viewData   = [
            'priceInfo' => $price_info
        ];
        $this->assign($viewData);
        return view();
    }


    //选中承运方
    public function select()
    {
        $user       = session("user");
        $usersInfo =  Db::name('users')->where('id',$user['id'])->find();
        if (request()->isAjax()) {
            $data      = input('post.');
            $orderInfo = Db::name('order')->where('order_number', $data['order_num'])->find();
            if (!empty($orderInfo['price_id'])) {
                $this->error('失败！该订单已经选中承运方');
            } else {
                $result = Db::name('order')
                    ->where('order_number', $data['order_num'])
                    ->update(['price_id' => $data['price_id'], 'status' => 2]);
                if ($result) {
                    $wechat_message = new Message();
                    $priceInfo = Db::name('price')->where('id',$data['price_id'])->find();
                    $expressInfo = Db::name('users')->where('id',$priceInfo['user_id'])->find();
                    $wechat_message->UserToExpress($expressInfo['openid'],$usersInfo['real_name'],$result['goods']);
                    $this->success('选中成功', 'wechat/index/index');
                } else {
                    $this->error('失败');
                }
            }
        }
        $price_info = model('price')::with('Order')
            ->where('id', input('id'))
            ->find();
        $viewData   = [
            'priceInfo' => $price_info
        ];
        $this->assign($viewData);
        return view();
    }

    //订单发货
    public function OrderChange(){
        $data['order_change'] = time();
        $data['status'] = 3;
        $result = Db::name('order')->where('id',input('post.id'))->update($data);
        if($result){
            $this->success('发货成功','wechat/index/index');
        }else{
            $this->error('发货失败');
        }
    }
}
